﻿using AutoMapper;
using AutoMapper.Configuration.Annotations;
using HealthcareSystem.API.Write.Application.Command.ElementCommand;
using HealthcareSystem.Domain.BasicSettings.Element_classification;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel.ElementCommandHandel
{
    public class PutElementCommandHandel : IRequestHandler<PutElementCommand, ApiResult<int>>
    {
        IBaseRepository<Managementelements> managementelementsHandle;
        IMapper mapper;

        public PutElementCommandHandel(IBaseRepository<Managementelements> managementelementsHandle, IMapper mapper)
        {
            this.managementelementsHandle = managementelementsHandle;
            this.mapper = mapper;
        }
        /// <summary>
        /// 元素修改处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        
        public async Task<ApiResult<int>> Handle(PutElementCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();

            var list = managementelementsHandle.GetAll().FirstOrDefault(x => x.ElementId == request.ElementId);
            var quer = mapper.Map(request, list);
            await managementelementsHandle.UpdAsync(quer);
            return await Task.FromResult(result);
        }
    }
}
